# -*- coding: UTF-8 -*-
# Spider for 'zara.comm'
#Author: Denis <sinedone@gmail.com>,

from urllib2 import unquote
from urlparse import urljoin

from scrapy.spider import BaseSpider
from scrapy.http import  Request
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.loader import XPathItemLoader
from scrapy.contrib.loader.processor import TakeFirst, Join
from scrapy.shell import inspect_response
from asos.items import ClothItem

class Zara(BaseSpider):

    name = 'zara'
    start_urls = ['http://www.zara.com/webapp/wcs/stores/servlet/home/us/en/zara-S2011-s']
    
    URL = 'http://www.etsy.com'
    
    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        for menu in hxs.select("//ul[@class='menuLevel1']//a"):
            entry = menu.select("text()").extract()[0].strip()
            if entry not in ['Woman', 'Man', 'Kids']:continue
            url = menu.select("@href").extract()[0]
            yield Request(url, meta={'sex':entry}, callback=self.list_categories)
    
    def list_categories(self, response):
#        inspect_response(response)
        hxs = HtmlXPathSelector(response)
        categories = hxs.select("//ul[@class='menuLevel3']//a") or\
                    hxs.select("//ul[@class='menuLevel2']//a")
        for cat in categories:
            category = cat.select("text()").extract()[0]
            url = cat.select("@href").extract()[0]
            yield Request(url, meta={'sex':response.request.meta['sex'],
                                     'category':category},
                          callback=self.list_items)
            
    def list_items(self, response):
        hxs = HtmlXPathSelector(response)
        category = response.request.meta['category']
        sex = response.request.meta['sex']
        base = hxs.select("//script/text()").re(r'"urlPrefix":"(.+?)"')[0]
        
        for item in hxs.select("//script/text()").re(r'"full":"(.+?)"'):
#            price = item.select(".//p[@class='price sale']/text()").extract()[0]
            url = urljoin(base, item)
            yield Request(url, meta={'category':category, 'sex':sex, },
                          callback=self.get_item)
        next_page = hxs.select("//li[@class='arrow']/a[contains(@class, 'next')]/@href").extract()
        if next_page and next_page[0].strip():
            yield Request(next_page[0], meta={'category':category, 'sex':sex},
                          callback=self.list_items)
    
    def get_item(self, response):
        hxs = HtmlXPathSelector(response)
        l = XPathItemLoader(ClothItem(), hxs)
        l.add_value('url', [unicode(response.url)])
        l.add_value('category', [response.request.meta['category']])
        price = hxs.select("//span[@class='sale']/text()").extract() or\
                hxs.select("//p[@id='pPrice']/text()").extract()
        l.add_value('price', price)
        l.add_xpath('title', "//h2/text()", TakeFirst())
        l.add_xpath('facebookURL', "//li[@class='facebook']/a/@href", TakeFirst(),
                    re=r"sharer.php\?u=(.+?)$")
        l.add_value('description', [u'\r\n'.join(i.strip() for i in 
                        hxs.select("//div[@class='prodInfoDesc']/p[not(@class)]/text()").extract()
                        if i.strip())])
        l.add_value('sex', [response.request.meta['sex']])
        images = hxs.select("//ul[@class='listH productImage']/li//input[@class='pAuxMZoom']/@value").extract()[:4]
        for i, image in enumerate(images):
            l.add_value('image%s' %str(i+1), image)
        item = l.load_item()
        if 'facebookURL' in item and item['facebookURL']:
            item['facebookURL'] = [unquote(item['facebookURL'][0])]
        return item
        
        